<template>
  <div>
    <!-- inline:行内表单模式 -->
    <el-form
      :inline="true"
      :model="dataForm"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.userName"
          placeholder="用户名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('sys:user:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button>
          <el-button v-if="isAuth('sys:user:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" style="width: 100%" v-loading="dataListLoading" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" width="55"> </el-table-column>
      <!-- show-overflow-tooltip:当内容过长被隐藏时显示 tooltip -->
      <el-table-column
        prop="userId"
        :show-overflow-tooltip="true"
        label="用户ID"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        :show-overflow-tooltip="true"
        label="用户名"
      >
      </el-table-column>
      <el-table-column
        prop="mobile"
        :show-overflow-tooltip="true"
        label="手机号"
      >
      </el-table-column>
      <el-table-column prop="status" :show-overflow-tooltip="true" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="updateUserStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="160px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:user:update')" size="mini" type="primary" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.userId)"></el-button>
          <el-button v-if="isAuth('sys:user:delete')" size="mini" type="danger" icon="el-icon-delete" @click="deleteHandle(scope.row.userId)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- size-change:pageSize 改变时会触发 返回值是每页条数
    page-size:每页显示条目个数，默认是10个
    current-change:currentPage 改变时会触发
    current-page:当前页数-->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './user-add-or-update'
export default {
  data() {
    return {
      dataForm: {
        userName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addOrUpdateVisible: false,
      dataListLoading: false,
      dataListSelections: []
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/user/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'username': this.dataForm.userName
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    //开关改变用户状态
    updateUserStatus(data) {
      let { userId, status } = data;
      //发送请求修改状态
      this.$http({
        url: this.$http.adornUrl("/sys/user/update"),
        method: "post",
        data: this.$http.adornData({ userId, status: status ? 1 : 0 }, false),
      }).then(({ data }) => {
        this.$message({
          message: "状态更新成功",
          type: "success",
        });
      });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },

    //删除
    deleteHandle(id){
      var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/user/delete'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
    }

  }

}
</script>

<style scoped>
</style>